#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    gtk_init();
    
    // 创建临时窗口确保 GDK 初始化
    GtkWidget *window = gtk_window_new();
    gtk_window_set_title(GTK_WINDOW(window), "监视器信息查看器");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
    
    // 获取默认显示
    GdkDisplay *display = gdk_display_get_default();
    if (!display) {
        g_printerr("无法获取默认显示\n");
        return 1;
    }
    
    // 获取监视器列表模型
    GListModel *monitors = gdk_display_get_monitors(display);
    if (!monitors) {
        g_printerr("无法获取监视器列表\n");
        return 1;
    }
    
    // 获取监视器数量
    guint n_monitors = g_list_model_get_n_items(monitors);
    g_print("=== 发现 %d 个监视器 ===\n\n", n_monitors);
    
    // 遍历所有监视器
    for (guint i = 0; i < n_monitors; i++) {
        // 获取监视器对象, 会增加对象的引用计数，使用完后需要调用者unref
        auto monitor = static_cast<GdkMonitor *>(g_list_model_get_item(monitors, i));
        //monitor = static_cast<GdkMonitor *>(g_list_model_get_item(monitors, i));
        
        // 获取基本信息
        const char *model = gdk_monitor_get_model(monitor);
        const char *manufacturer = gdk_monitor_get_manufacturer(monitor);
        const char *connector = gdk_monitor_get_connector(monitor);
        
        // 获取几何信息
        GdkRectangle geometry;
        gdk_monitor_get_geometry(monitor, &geometry);
        
        // 获取工作区域（减去任务栏等）
        GdkRectangle workarea;
        //gdk_monitor_get_workarea(monitor, &workarea);
        
        // 获取物理尺寸（毫米）
        int width_mm, height_mm;
        //gdk_monitor_get_physical_size(monitor, &width_mm, &height_mm);
        
        // 获取缩放因子
        int scale_factor = gdk_monitor_get_scale_factor(monitor);
        
        // 获取刷新率
        int refresh_rate = gdk_monitor_get_refresh_rate(monitor);
        
        // 检查是否为主监视器
        //gboolean is_primary = gdk_monitor_is_primary(monitor);
    
        
        // 打印信息
        g_print("监视器 #%d\n", i);
        g_print("----------------------------\n");
        g_print("型号: %s\n", model ? model : "未知");
        g_print("制造商: %s\n", manufacturer ? manufacturer : "未知");
        g_print("连接器: %s\n", connector ? connector : "未知");
        g_print("位置: (%d, %d)\n", geometry.x, geometry.y);
        g_print("分辨率: %d × %d\n", geometry.width, geometry.height);
        //g_print("工作区域: %d × %d @ (%d, %d)\n", 
        //       workarea.width, workarea.height, workarea.x, workarea.y);
        //g_print("物理尺寸: %d × %d mm\n", width_mm, height_mm);
        g_print("缩放因子: %d\n", scale_factor);
        g_print("刷新率: %.2f Hz\n", refresh_rate / 1000.0);
        //g_print("主监视器: %s\n", is_primary ? "是" : "否");
        g_print("\n");
        g_object_unref(monitor); //need to release by caller
        //g_object_unref(monitor);
        //g_object_unref(monitor);
        //g_object_unref(monitor);
        //g_object_unref(monitor);
    }
    
    // 启动主循环以保持窗口打开
    gtk_window_present(GTK_WINDOW(window));
    auto loop = g_main_loop_new(NULL, FALSE);
    g_signal_connect_swapped(window, "destroy", G_CALLBACK(g_main_loop_quit), loop);
    g_main_loop_run(loop);
    
    return 0;
}
